using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class forums_controls_forums_ForumThreads : Controls.DataManagerForumControl
{
    protected override void ItemChanged()
    {
        if (CurrentForum != null)
        {
            int page;
            if (!Helpers.QueryStringHelper.TryGetInt32("page", out page))
            {
                page = 1;
            }

            SetDataSource(DAL.Threads.GetByForumAndPage(CurrentForum, page).Items);
        }
        else
        {
            GridHasItems = false;
            gridThreads.DataSource = null;
            gridThreads.DataBind();
        }
    }

    public void SetDataSource(List<DAL.Thread> datasource)
    {
        GridHasItems = datasource.Count > 0;
        gridThreads.DataSource = datasource;
        gridThreads.DataBind();
    }

    public bool GridHasItems
    {
        get
        {
            if (ViewState["GridHasItems"] != null)
            {
                return (bool)ViewState["GridHasItems"];
            }
            return false;
        }
        protected set
        {
            ViewState["GridHasItems"] = value;
        }
    }

    protected string ThreadLocked(DAL.Thread.ThreadStatus status)
    {
        if (status == DAL.Thread.ThreadStatus.Locked)
        {
            return "(Locked)";
        }
        else
        {
            return "";
        }
    }

    protected string ThreadPriority(DAL.Thread.ThreadPriority priority)
    {
        if (priority == DAL.Thread.ThreadPriority.Sticky)
        {
            return "Sticky: ";
        }
        else
        {
            return "";
        }
    }

    public bool ShowHeader
    {
        get
        {
            if (ViewState["ShowHeader"] != null)
            {
                return (bool)ViewState["ShowHeader"];
            }
            return false;
        }
        set
        {
            ViewState["ShowHeader"] = value;
        }
    }

    protected string GetFirst500Chars(string input)
    {
        if (!String.IsNullOrEmpty(input))
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(500);
            // removing all YCodes
            input = Regex.Replace(input, @"\[.*?\]", "", RegexOptions.None);

            // removing superfluous line breaks
            input = Regex.Replace(input, "(?:\r?\n){2,}", "\n");

            // trimming excess characters
            if (input.Length >= 500)
            {
                input = input.Substring(0, 500);
            }

            // appending only the first 10 lines of the post
            string[] lines = input.Split('\n');
            int max = 10;
            if (lines.Length <= 10)
            {
                max = lines.Length;
            }
            for (int i = 0; i <= max - 1; i++)
            {
                sb.AppendLine(lines[i]);
            }

            // returning value
            return sb.ToString();
        }
        else
        {
            return "";
        }
    }
}
